/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 123
 * Date: 2023-06-04
 * Time: 19:58
 */
public class MyList {
    static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode createList(){
        ListNode A = new ListNode(3) ;
        ListNode B = new ListNode(3) ;
        ListNode C = new ListNode(3) ;
       A.next = B ;
       B.next = C ;
       C.next = null ;
       return A;
    }

    public ListNode partition(ListNode pHead, int x) {
        if(pHead == null){
            return null ;
        }
        int size = sizeNode(pHead);
        ListNode ret = pHead ;
        ListNode retNext = ret.next ;
        for(int i = 0 ; i < size -2 && retNext != null;i ++){
            if(retNext.val == x){
                addLast(pHead , retNext.val) ;
                ret.next = ret.next.next ;
                retNext = ret.next ;
            }else{
                ret = retNext ;
                retNext = retNext.next ;
            }
        }
        if(pHead .val == x) {
            pHead = pHead.next ;
        }
        return pHead ;

    }

    public int sizeNode(ListNode head){
        if(head == null){
            return 0;
        }
        int count = 0 ;
        ListNode ret = head ;
        while(ret != null){
            count ++ ;
            ret = ret.next ;
        }
        return count ;
    }
    public void addLast (ListNode head , int x){
        if(head == null){
            return ;
        }
        ListNode p = new ListNode(x) ;
        ListNode ret = head ;
        while(ret.next != null){
            ret = ret.next ;
        }
        ret.next = p ;
        p . next = null ;

    }



}
